package org.csu.pet_store_back.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.csu.pet_store_back.common.CommonResponse;
import org.csu.pet_store_back.domain.Product;
import org.csu.pet_store_back.domain.ProductInfo;
import org.csu.pet_store_back.persistence.CategoryMapper;
import org.csu.pet_store_back.persistence.ProductMapper;
import org.csu.pet_store_back.persistence.ProductpicinfoMapper;
import org.csu.pet_store_back.service.CatalogService;
import org.springframework.stereotype.Service;

import java.io.*;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductpicinfoMapper productpicinfoMapper;

    public CommonResponse<List<Product>> getProductList(String categoryId){
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category",categoryId);
        List<Product> productList=productMapper.selectList(queryWrapper);
        if(categoryId==null){
            return CommonResponse.createForError("参数出错");
        }
        else if(productList.size()==0){
            return CommonResponse.createForError("该商品大类没有商品小类");
        }
        else {
            return CommonResponse.createForSuccess("返回被点击大类下的商品小类列表",productList);
        }
    }

    public CommonResponse<Map> getProduct(String productId) throws SQLException, IOException {
//        System.out.println(1);
//        String s="C:\\Users\\17211\\Desktop\\petstore\\pet_store_front\\src\\assets\\MainPage\\fish4.gif";
//        File file = new File(s);
//        InputStream inputStream = new FileInputStream(file);
//        System.out.println(2);
//        byte[] buf=new byte[inputStream.available()];
//        //将文件读入到byte[]中
//        inputStream.read(buf);
//        QueryWrapper queryWrapper9=new QueryWrapper<>();
//        queryWrapper9.eq("productid","FI-SW-02");
//        ProductInfo productInfo=productpicinfoMapper.selectOne(queryWrapper9);
//        productInfo.setPicinfo(buf);
//        productpicinfoMapper.update(productInfo,queryWrapper9);


        Map<String,Object> map=new HashMap<>();

        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        Product product=productMapper.selectOne(queryWrapper);


        QueryWrapper<ProductInfo> queryWrapper_photo=new QueryWrapper<>();
        queryWrapper_photo.eq("productid",product.getProductId());
        ProductInfo photo=productpicinfoMapper.selectOne(queryWrapper_photo);
        String base64Image = Base64.getEncoder().encodeToString(photo.getPicinfo());
        map.put("product",product);
        map.put("photo",base64Image);






        if(productId==null){
            return CommonResponse.createForError("参数出错");
        }
        else if(product==null){
            return CommonResponse.createForError("指定商品不存在");
        }
        else {
            return CommonResponse.createForSuccess("返回单个商品小类",map);
        }
    }

    public CommonResponse<List<Product>> searchProductList(String keyword) {
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",keyword);
        List<Product> productList=productMapper.selectList(queryWrapper);
        if(keyword==null){
            return CommonResponse.createForError("参数出错");
        }
        else if(productList.size()==0){
            return CommonResponse.createForError("未查找到所需商品");
        }
        else {
            return CommonResponse.createForSuccess("返回符合查找要求的商品列表",productList);
        }

    }

}
